Ext.onReady(function() {
	var statisticStore = Ext.create('Ext.data.Store',{
		fields: [
		    {name: 'STATISTICKEY', type:'string'},
		    {name: 'MONEY', type:'float'},
		    {name: 'MONEY2', type:'float'},
		    {name: 'TYPE', type:'int'},
		    {name: 'DAY', type:'int'}
        ]
	});
	
	function getAllMoney(){
	    var allMoney = 0;
	    statisticStore.each(function(item){
	        allMoney += item.get("MONEY");
	    });
	    return allMoney;
	}
	
    var statisticChart = Ext.create('Ext.chart.Chart', {
        id: 'statisticChart',
        xtype: 'chart',
        style: 'background:#fff',
        animate: true,
        shadow: true,
        store: statisticStore,
        axes: [{
            type: 'Numeric',
            position: 'left',
            fields: ['MONEY', 'MONEY2'],
            label: {
                renderer: Ext.util.Format.numberRenderer('0,0')
            },
            grid: true,
            minimum: 0
        }, {
            type: 'Category',
            position: 'bottom',
            fields: ['STATISTICKEY']
        }],
        series: [{
            type: 'column',
            axis: 'left',
            highlight: true,
            tips: {
              trackMouse: true,
              width: 140,
              renderer: function(storeItem, item) {
            	  this.setTitle(storeItem.get('STATISTICKEY'));
            	  if(storeItem.get('TYPE') > 0){
            	      var money = storeItem.get('MONEY');
            	      var money2 = storeItem.get('MONEY2');
            	      var day = storeItem.get('DAY');
            	      var ret = 0;
            	      if(item.yField == "MONEY"){
            	          ret = Ext.util.Format.number(money/(money + money2)*100, "0.00") + "%";
            	          ret += "<br/>";
            	          ret += str_housekeeperstatistic_perday;
            	          ret += Ext.util.Format.number(money/day,"0.00");
            	      }
            	      else {
            	          ret = Ext.util.Format.number(money2/(money + money2)*100, "0.00") + "%";
            	      }
            	      ret = str_housekeeperstatistic_percent + ret;
                      this.update(ret);
            	  }
            	  else {
            	      var money = storeItem.get('MONEY');
            	      var sum = getAllMoney();
            	      var ret = str_housekeeperstatistic_allcost + Ext.util.Format.number(sum,"0.00");
            	      ret += "<br/>"
            	      ret += str_housekeeperstatistic_percent;
            	      ret += Ext.util.Format.number(money/sum*100,"0.00") + "%";
                      this.update(ret);
            	  }
              }
            },
            label: {
                display: 'outside',
                'text-anchor': 'middle',
                field: ['MONEY', 'MONEY2'],
                renderer: function(v){
                    if(v == 0){
                        return "";
                    }
                    return Ext.util.Format.number(v, '0.00')
                },
                color: '#333'
            },
            xField: 'STATISTICKEY',
            yField: ['MONEY', 'MONEY2']
        }]
    });
    
    var statByMonth = new Ext.create('Ext.button.Button',{
    	text: str_housekeeperstatistic_tjbymonth,
        pressed: true,
        handler: function() {
        	if (!this.pressed) {
        		toggleButton(this);
        		loadStatistic("MONTH");
        	}
        }
	});
    
    var statByMonthSplit = new Ext.create('Ext.button.Button',{
    	text: str_housekeeperstatistic_tjbymonthsplit,
        pressed: false,
        handler: function() {
        	if (!this.pressed) {
        		toggleButton(this);
        		loadStatistic("MONTHSPLIT:13");
        	}
        }
	});
    
    var statByType = new Ext.create('Ext.button.Button',{
    	text: str_housekeeperstatistic_tjbytype,
        pressed: false,
        handler: function() {
        	if (!this.pressed) {
        		toggleButton(this);
        		loadStatistic("SPENDTYPE");
        	}
        }
	});
    
    var toggleButtons = [statByMonth,statByType, statByMonthSplit];
    
    function toggleButton(btn) {
    	for(i = 0; i < toggleButtons.length; i++) {
    		if(toggleButtons[i].pressed){
    			toggleButtons[i].toggle();
    		}
    	}
    	btn.toggle();
    }
    
	var statYearStore = Ext.create('Ext.data.Store',{
		fields: [
		    {name: 'YEAR', type:'string'}
        ],
        proxy:{
			type: 'ajax',
			url : getPath("/ab/po/houseKeeperDetail.do"),
			extraParams : {
            	"md":"getYears"
            },
			reader : {
				type : 'json',
				root : 'year'
			}
		}
	});
    
	var statYear = Ext.create('Ext.form.field.ComboBox',{
        typeAhead: true,
        triggerAction: 'all',
        selectOnTab: true,
        editable: false,
        displayField: 'YEAR',
        valueField: 'YEAR',
		store: statYearStore,
        lazyRender: true,
        value:Ext.Date.format(new Date(), 'Y'),
        width:100,
        listeners:{
      	  select : function(combo, record,index) {
      		loadStatistic(currentStatType);
      	  }
        }
	});
    
	statisticWin = Ext.create('Ext.Window', {
		maximizable: true,
		iconCls: 'icon-bar',
        title: str_housekeeperstatistic_tjtu,
        height: 500,
        width: 800,
        closeAction: 'hide',
        layout: 'fit',
        items: [statisticChart],
        x: 80,
        y: 40,
        tbar: [{
            text: str_housekeeperstatistic_savedraft,
            handler: function() {
            	if(statisticStore.getTotalCount() != 0){
            		statisticChart.save({
            			type: 'image/png'
            		});
            	} 
            }
        },'-',{
      	    xtype: 'label',
    	    text: str_housekeeperstatistic_tjyear,
    	    width:80
          },statYear, '-', statByMonth,'-',statByType,'-',statByMonthSplit
        ],
        listeners:{
        	hide:function(){
        		if(statisticBtn.pressed){
        			statisticBtn.toggle();
        		}
        	}
        }
    });
    var currentStatType = "MONTH";	
	function loadStatistic(type) {
		currentStatType = type;
		statisticStore.setProxy({
			type : 'ajax',
			url : getPath("/ab/po/houseKeeperDetail.do"),
			extraParams : {
            	"md":"statistic",
            	"TYPE":type,
            	"YEAR":statYear.getValue()
            },
			reader : {
				type : 'json',
				root : 'statistic',
				totalProperty : 'totalCount'
			}
		});
		statisticStore.load();
	}
	
	statisticBtn = new Ext.create('Ext.button.Button',{
		text: str_housekeeperstatistic_tjtu,
		iconCls: 'icon-bar',
		pressed: false,
        enableToggle: true,
		listeners:{
			click: function(){
				if(statisticWin.isHidden()){
					statisticWin.show();
					if(statisticStore.getTotalCount() == 0){
						loadStatistic(currentStatType);
					}
					if(!chatwin.isHidden()) {
						chatwin.hide();
					}
					if(!gridwin.isHidden()) {
						gridwin.hide();
					}
				}
				else {
					statisticWin.hide();
				}
			}
		}
	});
});